/**
 * 
 */
package utility;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import wftElements.Activity;
import wftElements.ActivityAttribute;
import wftElements.ActivityAttributeValue;
import wftElements.ActivityTransition;
import wftElements.ItemType;
import wftElements.LookupType;
import wftElements.Message;
import wftElements.ProcessActivity;

import utility.Process;

import constants.*;

/**
 * @author ashutosh
 *
 */
public class WorkflowConversionUtil implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static String wrapTextInQuotes(String text){
		if(text != null){
			return OracleWorkflowComponents.QUOTES + text + OracleWorkflowComponents.QUOTES;
		}
		return "";
	}
	
	public static String getTextBetweenFirstQuotes(String text){
		if(text == null || text.equals("")){
			return text;
		}
		String tagRegEx = "\"[^\"]*\"";
		Pattern p2 = Pattern.compile(tagRegEx);
		Matcher m2 = p2.matcher(text);
		// Just counting all the tags first
		if (m2.find()) {
			return m2.group().replaceAll("\"", "");
		}
		return "";
	}
	
	public static String getTextAfterFirstQuote(String text){
		if(text == null || text.equals("")){
			return text;
		}
		String tagRegEx = "\"[^\"]*";
		Pattern p2 = Pattern.compile(tagRegEx);
		Matcher m2 = p2.matcher(text);
		// Just counting all the tags first
		if (m2.find()) {
			return m2.group().replaceAll("\"", "");
		}
		return "";
	}
	
	public static String getTextBeforeLastQuote(String text){
		if(text == null || text.equals("")){
			return text;
		}
		String tagRegEx = "[^\"]*\"";
		Pattern p2 = Pattern.compile(tagRegEx);
		Matcher m2 = p2.matcher(text);
		// Just counting all the tags first
		if (m2.find()) {
			return m2.group().replaceAll("\"", "");
		}
		return "";
	}
	
	public static String getTextsAfterEqualToSign(String text){
		if(text == null || text.equals("")){
			return text;
		}
		if(text.contains("= ")){
			return text.substring(text.indexOf("= ")+2);
		}
		return "";
	}
	
	public static ItemType getItemTypeFromItemTypeList(Process process, String itemName){
		List<ItemType> itemTypeList = process.getItemTypeList();
		if(itemName == null || itemTypeList == null){
			return null;
		}
		for(int index=0; index < itemTypeList.size(); index++){
			ItemType itemType = itemTypeList.get(index);
			if(itemName.equals(itemType.getName())){
				return itemType;
			}
		}
		return null;
	}
	
	public static String formatForMessage(String text){
		if(text == null || text.equals("")){
			return text;
		}
		if(text.contains("\\n\\")){
			text = text.replaceAll("\\n\\", "");
		}
		if(text.contains("\\n")){
			text = text.replaceAll("\\n", "");
		}
		if(text.endsWith("\\")){
			text = text.substring(0, text.length()-2);
		}
		
		if(text.contains("<br>") || text.contains("<BR>")){
			text = text.replaceAll("<BR>", "<BR/>");
		}
		return text;
	}
	
	public static Object[] getTextsBetweenQuotes(String text){
		List<String> strArr = new ArrayList<String>();
		if(text == null || text .equals("")){
			return (String[])strArr.toArray();
		}
		String tagRegEx = "\"[^\"]*\"";
		Pattern p2 = Pattern.compile(tagRegEx);
		Matcher m2 = p2.matcher(text);
		// Just counting all the tags first
		while(m2.find()) {
			strArr.add(m2.group().replaceAll("\"", ""));
		}
		return (Object[])strArr.toArray();
	}
	
	public static Activity getActivityByName(String actName, Process process){
		for (Activity act : process.getActList()) {
			if (act.getName().equalsIgnoreCase(actName)) {
				return act;
			}
		}
		return null;
	}
	
	public static Message getMessageByName(String msgName, Process process){
		for (Message msg : process.getMsgList()) {
			if (msg.getName().equalsIgnoreCase(msgName)) {
				return msg;
			}
		}
		return null;
	}
	
	public static LookupType getLookupTypeByName(String lookupTypeName, Process process){
		for (LookupType lookupType : process.getLookupTypeList()) {
			if (lookupType.getName().equalsIgnoreCase(lookupTypeName)) {
				return lookupType;
			}
		}
		return null;
	}
	
	public static ItemType getItemTypeByName(String itemTypeName, Process process){
		for (ItemType itemType : process.getItemTypeList()) {
			if (itemType.getName().equalsIgnoreCase(itemTypeName)) {
				return itemType;
			}
		}
		return null;
	}
	
	public static ProcessActivity getProcessActivityByName(String processActName, Activity act){
		for (ProcessActivity processAct : act.getProcessActivityList()) {
			if (processAct.getActivityName().equalsIgnoreCase(processActName)
					|| processAct.getLabel().equalsIgnoreCase(processActName)) {
				return processAct;
			}
		}
		return null;
	}
	
	public static ActivityAttribute getActivityAttributeByName(String actAttrName, Activity act){
		for (ActivityAttribute actAttr : act.getActivityAttrList()) {
			if (actAttr.getDisplayName().equalsIgnoreCase(actAttrName)) {
				return actAttr;
			}
		}
		return null;
	}
	
	public static ActivityTransition getActivityTransitionByName(String actTransName, ProcessActivity processAct){
		for (ActivityTransition actTransition : processAct.getActTransition()) {
			if (actTransition.getDisplayName().equalsIgnoreCase(actTransName)) {
				return actTransition;
			}
		}
		return null;
	}
	
	public static ActivityAttributeValue getActivityAttributeValueByName(String actAttrVal, ProcessActivity processAct){
		for (ActivityAttributeValue actAttrValue : processAct.getActAttrVal()) {
			if (actAttrValue.getValue().equalsIgnoreCase(actAttrVal)) {
				return actAttrValue;
			}
		}
		return null;
	}
	
	public static String getStringSplitForWhiteSpaces (String string){
		if(string == null){
			return string;
		}
		String strArr[] = string.split(" ");
		StringBuffer strBuff = new StringBuffer();
		for(String str : strArr){
			strBuff.append(str);
		}
		return strBuff.toString();
	}
}
